//
// Created by liaohailong on 2025/4/18.
//

#ifndef HELLOVIDEOCACHE_HELLOREQUESTHANDLER_HPP
#define HELLOVIDEOCACHE_HELLOREQUESTHANDLER_HPP

#include <unordered_set>

#include "../fetcher/HelloDataFetcher.hpp"
#include "../fetcher/HelloLocalFetcher.hpp"
#include "../fetcher/HelloNetworkFetcher.hpp"
#include "../log/Logger.hpp"

/**
 * create by liaohailong
 * 2025/4/18 21:46
 * desc: 请求处理基类
 */
class HelloRequestHandler
{

public:
    explicit HelloRequestHandler(const char *tag);
    virtual ~HelloRequestHandler();

    // 记录当前客户端的请求处理
    void addDataFetcher(int fd, const std::shared_ptr<HelloDataFetcher> &writer);
    void removeDataFetcher(int fd);
    void clearAndWaitAllFetcher();

    // 记录当前请求的url
    void addUrl(const std::string &url);
    void removeUrl(const std::string &url);
    bool hasUrl(const std::string &url);
    void clearUrls();

    virtual void acceptClient(std::shared_ptr<HelloHttpRequest> request) = 0;

protected:
    Logger logger;

    // 客户端持续写出
    std::mutex fetcherMutex;
    std::unordered_map<int, std::shared_ptr<HelloDataFetcher>> fetchers; // key=fd,value=数据获取器

    // 当前正在下载的资源,以 url 作为唯一标识
    std::mutex urlMutex;
    std::unordered_set<std::string> urls;
};


#endif //HELLOVIDEOCACHE_HELLOREQUESTHANDLER_HPP
